<%--
    Document   : Autocompleter tag
    Created on : Jun 4, 2010, 8:09:44 PM
    Author     : Sunil
--%>
<%@taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/tld/fn.tld" prefix="fn"%>
<%@taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@tag description="put the tag description here" pageEncoding="UTF-8"%>
<%@attribute name="name" required="true"%>
<%@attribute name="value"%>
<%@attribute name="label"%>
<%@attribute name="styleClass"%>
<%@attribute name="readOnly"%>
<%@attribute name="id"%>
<%@attribute name="validate"%>
<%@attribute name="table"%>
<%@attribute name="column"%>
<%@attribute name="columns"%>
<%@attribute name="container"%>
<%@attribute name="error_message"%>
<%@attribute name="disabled"%>
<%@attribute name="query"%>
<%@attribute name="URL"%>
<%@attribute name="maxlength"%>
<%@attribute name="fields"%>
<%@attribute name="callback"%>
<%@attribute name="template"%>
<%@attribute name="shouldMatch" type="java.lang.Boolean"%>
<%@attribute name="width" type="java.lang.Integer"%>
<c:catch var="exception">
    <c:if test="${id == null}" var="_var">
        <c:set var="id" value="${name}"/>
    </c:if>
    <c:if test="${container == null}">
        <c:set var="container" value="td"/>
    </c:if>
    <c:if test="${container != 'NULL'}">
        <${container}>
    </c:if>

    <c:if test="${label != null}">
        <label>
            ${label}
        </label>
    </c:if>

    <c:choose>
        <c:when test="${form != null}">
            <c:choose>
                <c:when test="${value != null}">
                    <html:text property="${name}" name="${form}" styleId="${id}" styleClass="text ${styleClass}" readonly="${readOnly}" value="${value}"  disabled="${disabled}"
                               maxlength="${maxlength}"/>
                </c:when>
                <c:otherwise>
                    <html:text property="${name}" name="${form}" styleId="${id}" styleClass="text ${styleClass}" readonly="${readOnly}"  disabled="${disabled}"
                               maxlength="${maxlength}"/>
                </c:otherwise>
            </c:choose>
        </c:when>
        <c:otherwise>
            <html:text property="${name}" styleId="${id}" styleClass="text ${styleClass}"  readonly="${readOnly}"  value="${value}"  disabled="${disabled}"
                       maxlength="${maxlength}"/>
        </c:otherwise>
    </c:choose>
    <c:if test="${shouldMatch}">
        <input type="hidden" name="${name}Check" id="${id}Check" value="${value}"/>
    </c:if>
    <c:choose>
        <c:when test="${empty query && (empty table || empty column)}">
            <cong:prompt text="Either query or table and column required" type="warning"/>
        </c:when>
        <c:otherwise>
            <c:if test="${not empty table && not empty column}">
                <c:set var="requiredColumns" value="${fn:split(columns,',')}"/>
                <c:set var="requiredFields" value="${fn:split(fields,',')}"/>
                <c:if test="${not empty requiredColumns && not empty requiredFields && fn:length(requiredColumns)!=fn:length(requiredFields)}">
                    <cong:prompt text="Both columns and fields should be same numbers" type="warning"/>
                </c:if>
            </c:if>
        </c:otherwise>
    </c:choose>

    <!-- if the attribute table and columns is not null then contruct URL -->
    <c:url var="URL" value="/action/getAutocompleterResults.jsp" scope="page">
        <c:choose>
            <c:when  test="${not empty query}">
                <c:param name="query" value="${query}"/>
            </c:when>
            <c:when  test="${not empty table && not empty column}">
                <c:param name="table" value="${table}"/>
                <c:param name="column" value="${column}"/>
                <c:param name="columns" value="${columns}"/>
            </c:when>
        </c:choose>
        <c:param name="template" value="${template}"/>
    </c:url>


    <!-- If URL is not null -->
    <c:if test="${URL != null}">
        <script>
            $().ready(function() {
                    $("#${id}").autocomplete("${URL}&",{
                        cellSeparator:"|",
                        width:"${width}",
                        shouldMatch:"${shouldMatch}",
                        noAuto : false,
                        onItemSelect:function(li){
                            var values = li.id.split("|");
                            $("#${id}").val(values[0]);
                            <c:set var="index" value="1"/>
                            <c:forTokens items="${fields}" delims="," var="field">
                                <c:if test="${field!='null'}">
                                    <c:choose>
                                        <c:when test="${fn:containsIgnoreCase(field,':Check')}">
                                            $("#${fn:replace(field,':Check','')}").val(values["${index}"]);
                                            $("#${fn:replace(field,':','')}").val(values["${index}"]);
                                        </c:when>
                                        <c:otherwise>
                                            $("#${field}").val(values["${index}"]);
                                        </c:otherwise>
                                    </c:choose>
                                </c:if>
                                <c:set var="index" value="${index+1}"/>
                            </c:forTokens>
                            <c:if test="${not empty callback}">
                                 eval("${callback}");
                            </c:if>    
                        },
                        callBefore:function(){
                            <c:forTokens items="${fields}" delims="," var="field">
                                <c:if test="${not empty field}">
                                    <c:choose>
                                        <c:when test="${fn:containsIgnoreCase(field,':Check')}">
                                            $("#${fn:replace(field,':Check','')}").val("");
                                            $("#${fn:replace(field,':','')}").val("");
                                        </c:when>
                                        <c:otherwise>
                                            $("#${field}").val("");
                                        </c:otherwise>
                                    </c:choose>
                                </c:if>
                            </c:forTokens>
                        }
                    });
                });
                $("#${id}").blur(function (){
                    if("${shouldMatch}"){
                        if($.trim($("#${id}").val())!=$.trim($("#${id}Check").val())){
                            $("#${id}").val("");
                            $("#${id}Check").val("");
                        }
                        <c:forTokens items="${fields}" delims="," var="field">
                            <c:if test="${not empty field}">
                                <c:choose>
                                    <c:when test="${fn:containsIgnoreCase(field,':Check')}">
                                        $("#${fn:replace(field,':Check','')}").val("");
                                        $("#${fn:replace(field,':','')}").val("");
                                    </c:when>
                                    <c:otherwise>
                                        $("#${field}").val("");
                                    </c:otherwise>
                                </c:choose>
                            </c:if>
                        </c:forTokens>
                    }
                });
        </script>
    </c:if>
    <%@include file="error.jsf" %>
    <jsp:doBody/>
    <c:if test="${container != 'NULL'}">
        </${container}>
    </c:if>
</c:catch>
<cong:prompt type="exception" text="${exception}"/>
